#!/usr/bin/env bash
set -euo pipefail

# pg_dumpall -d "$DB_URL" -gf global.sql --no-role-passwords

# Explanation of special flags:
#
#   --column-inserts  include migration history, but not pgsodium key
pg_dump \
    --column-inserts \
    --exclude-table-data 'pgsodium.key' \
    -d 'postgres://supabase_admin:postgres@127.0.0.1:54322/postgres' \
| sed 's/^CREATE SCHEMA /CREATE SCHEMA IF NOT EXISTS /' \
| sed 's/^CREATE TABLE /CREATE TABLE IF NOT EXISTS /' \
| sed 's/^CREATE SEQUENCE /CREATE SEQUENCE IF NOT EXISTS /' \
| sed 's/^CREATE VIEW /CREATE OR REPLACE VIEW /' \
| sed 's/^CREATE FUNCTION /CREATE OR REPLACE FUNCTION /' \
| sed 's/^GRANT ALL ON FUNCTION graphql_public./-- GRANT ALL ON FUNCTION graphql_public./'

echo "DROP EXTENSION pg_graphql; CREATE EXTENSION pg_graphql WITH SCHEMA graphql;"

# Reset session config generated by pg_dump
echo "RESET ALL;"
